package com.xmb.wechat.view.wechat.chat;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;

import com.blankj.utilcode.util.ToastUtils;
import com.squareup.picasso.Picasso;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.application.WechatStartApplication;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.bean.WechatLastMsgBean;
import com.xmb.wechat.common.Constant;
import com.xmb.wechat.definterface.PicChooseWithCutCallBack;
import com.xmb.wechat.definterface.SimpleTextWatcher;
import com.xmb.wechat.definterface.onBgChooseListener;
import com.xmb.wechat.dialog.BGSelectDailog;
import com.xmb.wechat.util.PicChooseUtils;
import com.xmb.wechat.util.PicLoadUtils;
import com.xmb.wechat.widget.TitleLayout;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author lijiangop
 * @CreateTime 2020/6/8 15:52
 */
public class WechatDanliaoEditActivity extends BaseActivity {

    private static final int REQUEST_CODE_IMAGE = 123;

    @BindView(R2.id.title_layout)
    TitleLayout mTitleLayout;
    @BindView(R2.id.iv_avatar)
    ImageView   ivAvatar;
    @BindView(R2.id.et_name)
    EditText    etName;
    @BindView(R2.id.iv_bg)
    ImageView   mIvBg;
    @BindView(R2.id.switch_no_dis)
    Switch      mSwitchNoDis;
    @BindView(R2.id.switch_notice)
    Switch      mSwitchNotice;

    private WechatContactBean mContact;
    private BGSelectDailog    mBgSelectDialog;

    public WechatDanliaoEditActivity() {
        super(R.layout.activity_danliao_edit);
    }

    public static void start4result(Activity act, WechatLastMsgBean wechatLastMsgBean, int requestCode) {
        Intent intent = new Intent(act, WechatDanliaoEditActivity.class);
        intent.putExtra("talkerId", wechatLastMsgBean.getTalkerID());
        act.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void initView() {
        mContact = WechatStartApplication.getBoxStore().boxFor(WechatContactBean.class).get(getIntent().getLongExtra("talkerId",0));
        if (mContact == null)
            return;
        mTitleLayout.setRightClick(v -> checkValueUsable());
        mSwitchNoDis.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (mContact != null)
                    mContact.setMute(isChecked);
            }
        });
        mSwitchNotice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (mContact != null)
                    mContact.setStrongRemind(isChecked);
            }
        });
        etName.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChangedFinish(String value) {
                if (mContact != null) {
                    mContact.setName(value);
                }
            }
        });
        WechatContactBean.setupAvatarIntoImageView(mContact, ivAvatar);
        etName.setText(TextUtils.isEmpty(mContact.getName()) ? "" : mContact.getName());
        if (!TextUtils.isEmpty(mContact.getChatBG()))
            PicLoadUtils.loadChatBg(mContact.getChatBG(), mIvBg);
        mSwitchNoDis.setChecked(mContact.isMute());
        mSwitchNotice.setChecked(mContact.isStrongRemind());
    }

    @OnClick({R2.id.btn_pick_avatar, R2.id.ll_bg, R2.id.btn_add})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (view.getId() == R.id.btn_pick_avatar) {
            PicChooseUtils.chooseOnePicWithCut(this, 1, 1, Pic -> {
                mContact.setAvatar(Pic[0]);
                mContact.setAvatarSrc(Constant.SourceType.AVATAR_SRC_SDCARD);
                ivAvatar.setImageBitmap(BitmapFactory.decodeFile(Pic[0]));
            });
        } else if (id == R.id.ll_bg) {
            if (mBgSelectDialog == null) {
                mBgSelectDialog = new BGSelectDailog(WechatDanliaoEditActivity.this, new onBgChooseListener() {

                    @Override
                    public void onDefaultChoose() {
                        mContact.setChatBG("default");
                        Picasso.get().load(R.drawable.img_default_gray).into(mIvBg);
                    }

                    @Override
                    public void onUserDefinedChoose() {
                        PicChooseUtils.chooseOnePicWithCut(WechatDanliaoEditActivity.this, new PicChooseWithCutCallBack() {
                            @Override
                            public void onPicChoose(String... Pic) {
                                if (TextUtils.isEmpty(Pic[0]))
                                    return;
                                PicLoadUtils.loadSdcardPic(Pic[0], mIvBg);
                                if (mContact != null)
                                    mContact.setChatBG(Pic[0]);
                            }
                        });
                    }
                });
            }
            mBgSelectDialog.show();
        } else if (id == R.id.btn_add) {
            checkValueUsable();
        }
    }

    private void checkValueUsable() {
        //存入数据库
        WechatStartApplication.getBoxStore().boxFor(WechatContactBean.class).put(mContact);//保存群聊数据
        ToastUtils.showShort("保存成功！");
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBgSelectDialog != null) {
            mBgSelectDialog.dismiss();
            mBgSelectDialog = null;
        }
    }
}
